package com.transaction.demo.order.controller;

import java.util.*;
public class PrimeNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("请输入要判断的最大值：");
        int maxNum = scanner.nextInt();
        
        List<Integer> primeNumbers = getPrimeNumbers(maxNum);
        
        if (primeNumbers != null && !primeNumbers.isEmpty()) {
            System.out.println("在 " + maxNum + " 内的质数有：" + Arrays.toString(primeNumbers.toArray()));
        } else {
            System.out.println("没有找到任何质数。");
        }
    }
    
    private static List<Integer> getPrimeNumbers(int maxNum) {
        List<Integer> primeNumbers = new ArrayList<>();
        
        for (int i = 2; i <= maxNum; i++) {
            boolean isPrime = true;
            
            // 使用平方根来优化性能
            double sqrtMaxNum = Math.sqrt((double)i);
            
            for (int j = 2; j <= sqrtMaxNum; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            
            if (isPrime) {
                primeNumbers.add(i);
            }
        }
        
        return primeNumbers;
    }
}